package cn.dankal.flyaudio.personal;

import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.kongzue.dialog.v2.SelectDialog;

import java.util.List;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.callback.DKCallBackBoolean;
import cn.dankal.basiclib.pojo.FavoritesShopEntity;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.widget.CommonDialog;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.MyCollectionAdapter;
import cn.dankal.flyaudio.contract.MyCollectionContract;
import cn.dankal.flyaudio.presenter.MyCollectionPresenter;

public class MyCollectionActivity extends BaseActivity implements MyCollectionContract.View {
    private RecyclerView listView;

    private MyCollectionAdapter adapter;

    private View bottomHandle;

    private TextView editView;
    private TextView tvBottomSelect;
    private TextView tvDelete;
    private boolean isEditModel = false;

    private MyCollectionPresenter myCollectionPresenter;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myCollectionPresenter.detachView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collection;
    }

    @Override
    protected void initComponents() {
        listView = findViewById(R.id.rv_collection_store);
        editView = findViewById(R.id.tv_edit);
        adapter = new MyCollectionAdapter();
        myCollectionPresenter = new MyCollectionPresenter();
        myCollectionPresenter.attachView(this);
        bottomHandle = findViewById(R.id.ll_bottom_handle);
        listView.setLayoutManager(new LinearLayoutManager(this));
        listView.setAdapter(adapter);
        adapter.clearData();
        adapter.closePickModel();
        isEditModel = false;
        bottomHandle.setVisibility(View.GONE);
        editView.setText("编辑");
        myCollectionPresenter.getDataList();
        tvBottomSelect = findViewById(R.id.tv_bottom_select);
        tvDelete = findViewById(R.id.tv_delete);

        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        tvBottomSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                isEditModel = v.isSelected();
                for (FavoritesShopEntity data : adapter.getDatas()) {
                    data.setSelect(v.isSelected());
                }
                if (v.isSelected()) {
                    adapter.setPickModel();
                } else {
                    adapter.setPickModel();
                }
            }
        });
        editView.setOnClickListener(v -> {
            if (!isEditModel) {
                isEditModel = true;
                editView.setText("完成");
                adapter.setPickModel();
                bottomHandle.setVisibility(View.VISIBLE);
            } else {
                adapter.closePickModel();
                isEditModel = false;
                bottomHandle.setVisibility(View.GONE);
                editView.setText("编辑");
            }
        });

        LiveEventBus.get("MyCollectionDelete", String.class).observe(this,
                new Observer<String>() {
                    @Override
                    public void onChanged(String s) {
                        SelectDialog.show(MyCollectionActivity.this, "提示", "是否要删除", "确定", (dialog, which) -> {
                            myCollectionPresenter.cancelFavoritesProduct(s);
                        }, "取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                    }
                });
        LiveEventBus.get("MyCollection", boolean.class).observe(
                this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        boolean isAll = true;
                        for (FavoritesShopEntity data : adapter.getDatas()) {
                            if (!data.isSelect()) {
                                isAll = false;
                            }
                        }
                        tvBottomSelect.setSelected(isAll);
                    }
                }
        );
    }

    private CommonDialog commonDialog;

    private void showDialog() {
        CommonDialog.Builder builder = new CommonDialog.Builder(this);
        builder.setTitle("提示");
        builder.setDes("是否删除收藏");
        builder.setDesColor(getResources().getColor(R.color.color00));
        builder.setEditVisibility(false);

        builder.setNegativeButton("取消", getResources().getColor(R.color.color00), new DKCallBackBoolean() {
            @Override
            public void action(int type) {
                commonDialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", getResources().getColor(R.color.color_0085B4),
                new DKCallBackBoolean() {
                    @Override
                    public void action(int type) {
                        commonDialog.dismiss();
                        StringBuilder sb = new StringBuilder();
                        for (FavoritesShopEntity data : adapter.getDatas()) {
                            if (data.isSelect()) {
                                sb.append(data.getProductUuid()).append(",");
                            }
                        }
                        if (sb.length() > 0) {
                            sb.deleteCharAt(sb.length() - 1);
                        }
                        if (sb.length() == 0) {
                            ToastUtils.showShort("请选择商品删除");
                            return;
                        }
                        myCollectionPresenter.cancelFavoritesProduct(sb.toString());

                    }
                });

        commonDialog = builder.create();
        commonDialog.show();
    }

    @Override
    public void addSuccess(List<FavoritesShopEntity> list) {
        adapter.addDatas(list, true);
    }

    @Override
    public void cancelFavoritesProductSuccess() {
        adapter.clearData();
        myCollectionPresenter.getDataList();
        LiveEventBus.get("update_user_info", String.class).post("");
    }
}
